package com.elepro.shoppingmall.service.orders;

import com.elepro.shoppingmall.entity.Goods;
import com.elepro.shoppingmall.entity.orders.Order;
import com.elepro.shoppingmall.entity.orders.OrderGoods;
import com.elepro.shoppingmall.entity.orders.OrdersInfo;
import com.elepro.shoppingmall.entity.orders.ShoppingCart;
import com.elepro.shoppingmall.mapper.goods.GoodsMapper;
import com.elepro.shoppingmall.mapper.orders.OrderMapper;
import com.elepro.shoppingmall.utils.Finance.FinanceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceIm implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查找所有的订单
     * @param userId
     * @return
     */
    @Override
    public List<Order> getOrderByUserId(Long userId) {

        return orderMapper.getOrderByUserId(userId);
    }

    /**
     * 返回orderGoods的信息
     * @param orderId
     * @return
     */
    @Override
    public List<OrderGoods> getOrderGoodsByOrderId(String orderId) {
        return orderMapper.getOrderGoodsByOrderId(orderId);
    }

    /**
     * 插入订单
     * @param shoppingCartList
     * @return
     */
    public String insertOrder(List<ShoppingCart> shoppingCartList){
        if (shoppingCartList == null )
            return "fail";
        Order order = new Order();

        order.setAddress("待定");
        order.setUserId(shoppingCartList.get(0).getUserId());
        order.setOrderId(FinanceUtils.getUUIDToString());
        Double moneys = new Double(0);
        for (ShoppingCart shoppingCart : shoppingCartList){
            moneys += shoppingCart.getMoneys();
            //insert orderGoods
            orderMapper.insertOrderGoods(new OrderGoods(order.getOrderId(),shoppingCart.getGoodsId(),shoppingCart.getGoodsAmount()));
        }

        order.setMoneys(moneys);
        order.setTime(new Date());
        orderMapper.insertOrder(order);
        return "success";
    }

    /**
     * 查询一订单中的n商品
     * @param orderId
     * @return
     */
    public List<OrdersInfo> getOrderInfoList(String orderId){
        List<OrderGoods> orderGoodsList = getOrderGoodsByOrderId(orderId);

        List<OrdersInfo> ordersInfoList = new ArrayList<>();

        for (OrderGoods orderGoods : orderGoodsList){
            Goods goods = goodsMapper.getGoodsById(orderGoods.getGoodsId());
            String goodsType = goodsMapper.getGoodsTypeByTypeId(goods.getGoodsTypeId()).getGoodsType();
            Double moneys = orderGoods.getGoodsAmount()*goods.getPrice();
            OrdersInfo ordersInfo = new OrdersInfo(
                    goods.getId(),
                    goods.getGoodsName(),
                    goods.getPrice(),
                    goodsType,
                    orderGoods.getGoodsAmount(),
                    moneys,
                    goods.getPicture()
            );

            ordersInfoList.add(ordersInfo);
        }

        return ordersInfoList;
    }


}
